/*
   Copyright (c) 2011, 2012 Mizar Tools Contributors (mizartools.org)

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
/*  Contributors :
 *	2011-02-18 Marco Riccardi - initial implementation
 *  2012-04-06 Marco Riccardi - added message in DliParseException
 *
 */
package org.mizartools.dli;

public class FunctorNotation extends ItemDefinition {

	private SymbolId symbolId1 = null;
	private SymbolId symbolId2 = null;
	
	private Loci loci = null;
	private Format format = null;
	private Visible visible = null;
	private Constructor constructor = null;
	private Synonym synonym = null;
	
	private FunctorNotation(){
		super(null);
	}

    public FunctorNotation(SymbolId symbolId1, SymbolId symbolId2, Loci loci, Format format, Visible visible, Constructor constructor, Synonym synonym) throws DliException {
    	super(null);
		this.symbolId1 = symbolId1;
		this.symbolId2 = symbolId2;
		this.loci = loci;
		this.format = format;
		this.visible = visible;
		this.constructor = constructor;
		this.synonym = synonym;
    }
	
	public static ItemDefinition newInstance(String string) throws DliParseException {
		FunctorNotation functorNotation = new FunctorNotation();
		int position = string.indexOf("(");
		if (position == -1) {
			throw new DliParseException("["+string+"] is not valid in FunctorNotation");
		}
		if (!string.substring(0, position).equals("funcnot")) {
			throw new DliParseException("["+string+"] is not valid in FunctorNotation");
		}
		string = string.substring(position+1, string.length()-1);
		int positionComma = Utility.searchSeparationComma(string);
		if (positionComma == -1) {
			throw new DliParseException("["+string+"] is not valid in FunctorNotation");
		}
		functorNotation.symbolId1 = SymbolId.newInstance(string.substring(0, positionComma));
		string = string.substring(positionComma+1, string.length());
		positionComma = Utility.searchSeparationComma(string);
		if (positionComma == -1) {
			throw new DliParseException("["+string+"] is not valid in FunctorNotation");
		}
		if (functorNotation.symbolId1.symbolType == SymbolType.vocK){
			functorNotation.symbolId2 = SymbolId.newInstance(string.substring(0, positionComma));
			string = string.substring(positionComma+1, string.length());
			positionComma = Utility.searchSeparationComma(string);
		}
		String type = null;
		while (positionComma != -1) {
			position = string.indexOf("(");
			int positionCommaTest = string.indexOf(",");
			if (positionCommaTest != 0 && positionComma<position){
				position = positionCommaTest;
			}
			if (position == -1) {
				throw new DliParseException("["+string+"] is not valid in FunctorNotation");
			}
			type = string.substring(0, position);
			if (type.equals("$loci")){
				functorNotation.loci = Loci.newInstance(string.substring(0, positionComma));
			} else if (type.equals("$format")){
				functorNotation.format = Format.newInstance(string.substring(0, positionComma));
			} else if (type.equals("$visible")){
				functorNotation.visible = Visible.newInstance(string.substring(0, positionComma));
			} else if (type.equals("$constructor")){
				functorNotation.constructor = Constructor.newInstance(string.substring(0, positionComma));
			} else if (type.equals("$synonym")){
				functorNotation.synonym = Synonym.newInstance(string.substring(0, positionComma));
			} else {
				throw new DliParseException("["+string+"] is not valid in FunctorNotation");
			}
			string = string.substring(positionComma+1, string.length());
			positionComma = Utility.searchSeparationComma(string);
		}
		if (string.equals("$synonym")){
			functorNotation.synonym = Synonym.newInstance(string);
		} else {
			position = string.indexOf("(");
			if (position == -1) {
				throw new DliParseException("["+string+"] is not valid in FunctorNotation");
			}
			type = string.substring(0, position);
			if (type.equals("$loci")){
				functorNotation.loci = Loci.newInstance(string);
			} else if (type.equals("$format")){
				functorNotation.format = Format.newInstance(string);
			} else if (type.equals("$visible")){
				functorNotation.visible = Visible.newInstance(string);
			} else if (type.equals("$constructor")){
				functorNotation.constructor = Constructor.newInstance(string);
			} else if (type.equals("$synonym")){
				functorNotation.synonym = Synonym.newInstance(string);
			} else {
				throw new DliParseException("["+string+"] is not valid in FunctorNotation");
			}
		}
		return functorNotation;
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();
		sb.append("funcnot(");
		if (symbolId1 != null){
			sb.append(symbolId1.toString());
		}
		if (symbolId2 != null){
			sb.append(",");
			sb.append(symbolId2.toString());
		}
		if (loci != null){
			sb.append(",");
			sb.append(loci.toString());
		}
		if (format != null){
			sb.append(",");
			sb.append(format.toString());
		}
		if (visible != null){
			sb.append(",");
			sb.append(visible.toString());
		}
		if (constructor != null){
			sb.append(",");
			sb.append(constructor.toString());
		}
		if (synonym != null){
			sb.append(",");
			sb.append(synonym.toString());
		}
		sb.append(")");
		return sb.toString();
		
	}

	public SymbolId getSymbolId1() {
		return symbolId1;
	}

	public SymbolId getSymbolId2() {
		return symbolId2;
	}

	public Loci getLoci() {
		return loci;
	}

	public Format getFormat() {
		return format;
	}

	public Visible getVisible() {
		return visible;
	}

	public Constructor getConstructor() {
		return constructor;
	}

	public Synonym getSynonym() {
		return synonym;
	}
	
}
